package com.cgbl.cg.ui.resource

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import androidx.core.view.isNotEmpty
import androidx.core.widget.doOnTextChanged
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.LogUtils
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityResourceFilterBinding
import com.cgbl.cg.vm.resource.FilterResourceViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.FilterBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.arouter.ARouterUtil
import com.robot.base.view.CgTextView
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter

@Route(path = AppConstants.Router.RESOURCE.A_FILTER_RESOURCE)
class FilterResourceActivity :
    GLBaseActivity<AppActivityResourceFilterBinding, FilterResourceViewModel>() {
    var finishType = false
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_resource_filter

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initAdapter()
    }

    override fun initParam() {
        super.initParam()
        finishType = intent.getBooleanExtra("finishType", false)
    }

    override fun initData() {
        super.initData()
        viewModel.finishType = finishType
    }

    private fun initAdapter() {
        binding.editMinPrice.doOnTextChanged { text, start, before, count ->
            if (!TextUtils.isEmpty(text)) {
                binding.flowLayoutPrice.onChanged()
            }
        }
        binding.editMaxPrice.doOnTextChanged { text, start, before, count ->
            if (!TextUtils.isEmpty(text)) {
                binding.flowLayoutPrice.onChanged()
            }
        }
        val priceAdapter = object : TagAdapter<String>(viewModel.priceList) {
            override fun getView(parent: FlowLayout, position: Int, s: String?): View {
                val flowLayout = LayoutInflater.from(this@FilterResourceActivity).inflate(
                    R.layout.app_flow_filter_text,
                    binding.flowLayoutPrice, false
                ) as CgTextView
                flowLayout.text = s
                return flowLayout
            }
        }
        binding.flowLayoutPrice.adapter = priceAdapter
        binding.flowLayoutPrice.setOnTagClickListener { view, position, parent ->
            viewModel.minPrice.set("")
            viewModel.maxPrice.set("")
            binding.editMinPrice.clearFocus()
            binding.editMaxPrice.clearFocus()
            return@setOnTagClickListener true
        }

        val sidesAdapter = object : TagAdapter<String>(viewModel.sidesList) {
            override fun getView(parent: FlowLayout, position: Int, s: String?): View {
                val flowLayout = LayoutInflater.from(this@FilterResourceActivity).inflate(
                    R.layout.app_flow_filter_text,
                    binding.flowLayoutPrice, false
                ) as CgTextView

                flowLayout.text = s
                return flowLayout
            }
        }
        binding.flowLayoutSides.adapter = sidesAdapter

        val wiringAdapter = object : TagAdapter<String>(viewModel.wiringList) {
            override fun getView(parent: FlowLayout, position: Int, s: String?): View {
                val flowLayout = LayoutInflater.from(this@FilterResourceActivity).inflate(
                    R.layout.app_flow_filter_text,
                    binding.flowLayoutPrice, false
                ) as CgTextView

                flowLayout.text = s
                return flowLayout
            }
        }
        binding.flowLayoutWiring.adapter = wiringAdapter

        val formatAdapter = object : TagAdapter<String>(viewModel.formatList) {
            override fun getView(parent: FlowLayout, position: Int, s: String?): View {
                val flowLayout = LayoutInflater.from(this@FilterResourceActivity).inflate(
                    R.layout.app_flow_filter_text,
                    binding.flowLayoutPrice, false
                ) as CgTextView

                flowLayout.text = s
                return flowLayout
            }
        }

        binding.flowLayoutFormat.adapter = formatAdapter
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.cancelEvent.observe(this) {
            viewModel.minPrice.set("")
            viewModel.maxPrice.set("")
            binding.flowLayoutFormat.onChanged()
            binding.flowLayoutWiring.onChanged()
            binding.flowLayoutPrice.onChanged()
            binding.flowLayoutSides.onChanged()
        }

        viewModel.uc.confirmEvent.observe(this) {
            val filterBean = FilterBean()

            if (binding.flowLayoutPrice.selectedList.isNotEmpty()) {
                val bean = FilterBean.FilterChildBean()
                bean.id = "${binding.flowLayoutPrice.selectedList.iterator().next() + 1}"
                bean.text =
                    viewModel.priceList[binding.flowLayoutPrice.selectedList.iterator().next()]
                bean.type = AppConstants.BundleKey.PRICE
                filterBean.isFree = bean
            } else {
                if (!TextUtils.isEmpty(viewModel.minPrice.get()) || !TextUtils.isEmpty(viewModel.maxPrice.get())) {
                    val bean = FilterBean.FilterChildBean()
                    bean.id = "0"
                    bean.text = "${viewModel.minPrice.get()}-${viewModel.maxPrice.get()}"
                    bean.type = AppConstants.BundleKey.PRICE
                    filterBean.isFree = bean
                }
            }
            if (binding.flowLayoutSides.selectedList.isNotEmpty()) {
                val bean = FilterBean.FilterChildBean()
                bean.id = "${binding.flowLayoutSides.selectedList.iterator().next()}"
                bean.text =
                    viewModel.sidesList[binding.flowLayoutSides.selectedList.iterator().next()]
                bean.type = AppConstants.BundleKey.MS
                filterBean.ms = bean
            }

            if (binding.flowLayoutWiring.selectedList.isNotEmpty()) {
                val bean = FilterBean.FilterChildBean()
                bean.type = AppConstants.BundleKey.WIRING_Type
                bean.id = "${binding.flowLayoutWiring.selectedList.iterator().next()}"
                bean.text =
                    viewModel.wiringList[binding.flowLayoutWiring.selectedList.iterator()
                        .next()]
                filterBean.wiringType = bean
            }

            if (binding.flowLayoutFormat.selectedList.isNotEmpty()) {
                val bean = FilterBean.FilterChildBean()
                bean.type = AppConstants.BundleKey.RESOURCES_TYPE

                bean.id = "${binding.flowLayoutFormat.selectedList.iterator().next()}"
                bean.text =
                    viewModel.formatList[binding.flowLayoutFormat.selectedList.iterator()
                        .next()]
                filterBean.resourcesType = bean
            }
            LiveDataEventManager.postResourceFilterEvent(filterBean)
            if (!finishType) {
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.RESOURCE.A_RESOURCE_MENU,
                    Bundle().apply {
                        putSerializable(AppConstants.BundleKey.FILTER_RESOURCE_BEAN, filterBean)
                    }
                )
            }
            finish()
        }
    }
}